package com.yundin.service.impl;

import com.yundin.constant.JwtClaimsConstant;
import com.yundin.context.BaseContext;
import com.yundin.dto.ShoppingCartDTO;
import com.yundin.entity.Dish;
import com.yundin.entity.Setmeal;
import com.yundin.entity.ShoppingCart;
import com.yundin.mapper.DishMapper;
import com.yundin.mapper.SetmealMapper;
import com.yundin.mapper.ShoppingCartMapper;
import com.yundin.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;


@Service
@Slf4j
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    /**
     * 添加到购物车
     * @param shoppingCartDTO
     */
    @Override
    @Transactional
    public void addShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        log.info("添加到购物车：{}",shoppingCartDTO);
        ShoppingCart shoppingCart =new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        //购物车中设置用户id
        shoppingCart.setUserId(BaseContext.getCurrentId());
        //先取出当前用户的购物车数据
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.list(shoppingCart);
        if(shoppingCartList!=null && shoppingCartList.size()==1){
            //有购物车数据，将数量更新
            shoppingCart = shoppingCartList.get(0);
            shoppingCart.setNumber(shoppingCart.getNumber()+1);
            //更新数据库
            shoppingCartMapper.updateNumberById(shoppingCart);
        }else {
            //没有当前用户数据
            Long dishId = shoppingCartDTO.getDishId();
            if(dishId !=null){
                //添加菜品数据
                Dish dish = dishMapper.getById(dishId);
                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setAmount(dish.getPrice());
            }else {
                //套餐数据添加
                Long setmealId = shoppingCartDTO.getSetmealId();
                Setmeal setmeal = setmealMapper.getById(setmealId);
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setAmount(setmeal.getPrice());
            }
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartMapper.insert(shoppingCart);
        }
    }

    @Override
    public List<ShoppingCart> showShoppingCart() {
        return shoppingCartMapper.list(ShoppingCart.builder()
                .userId(BaseContext.getCurrentId()).build());
    }

    @Override
    public void cleanShoppingCart() {
        shoppingCartMapper.deleteByUserId(BaseContext.getCurrentId());
    }

    @Override
    public void subShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        log.info("删除购物车中的商品：{}",shoppingCartDTO);
        ShoppingCart shoppingCart =new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        //购物车中设置用户id
        shoppingCart.setUserId(BaseContext.getCurrentId());
        //先取出当前用户的购物车数据
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.list(shoppingCart);
        if(shoppingCartList!=null && shoppingCartList.size()==1){
            shoppingCart = shoppingCartList.get(0);
            if(shoppingCart.getNumber()==1){
                //直接删除
                shoppingCartMapper.deleteById(shoppingCart.getId());
            }else {
                //更新数量
                shoppingCart.setNumber(shoppingCart.getNumber()-1);
                shoppingCartMapper.updateNumberById(shoppingCart);
            }
        }
    }
}
